/**
 * 
 */
package com.gmail.avereshchaga.db.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gmail.avereshchaga.db.dao.IEmployeeDAO;
import com.gmail.avereshchaga.db.entity.IEmployee;

/**
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 * 
 */
@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private Integer itemsPerPage = 20;

    @Autowired
    private IEmployeeDAO employeeDAO;

    private boolean msg;
    private Integer quantityOfEntities;
    private List<Integer> pages;
    private String page;

    @Transactional
    public void createEmployee(IEmployee employee) {
        employeeDAO.createEmployee(employee);
    }

    @Transactional
    public IEmployee readEmployee(Long id) {
        return employeeDAO.readEmployee(id);
    }

    @Transactional
    public void updateEmployee(IEmployee employee) {
        employeeDAO.updateEmployee(employee);
    }

    @Transactional
    public List<IEmployee> findAll(String param, String pageStr) {
        
        page = pageStr;

        if (param == null || param.isEmpty()) {
            param = "%";
        }
        if (pageStr == null) {
            pageStr = "0";
        }

        String paramInSQLSyntax = param.replace("*", "%")
                .replaceAll("\\?", "_");
        Integer page = Integer.parseInt(pageStr);

        quantityOfEntities = employeeDAO
                .definitionQuantityOfEntities(paramInSQLSyntax);
        pages = listOfPages(quantityOfEntities);

        ArrayList<IEmployee> employees = (ArrayList<IEmployee>) employeeDAO
                .findAll(paramInSQLSyntax, page);
        if (employees.isEmpty()) {
            msg = true;
        } else {
            msg = false;
        }
        return employees;
    }

    public List<Integer> listOfPages(Integer quontityOfEntities) {
        List<Integer> listOfPages = new ArrayList<Integer>();
        Integer numberOfPages;
        if (quontityOfEntities % itemsPerPage == 0) {
            numberOfPages = quontityOfEntities / itemsPerPage;
        } else {
            numberOfPages = quontityOfEntities / itemsPerPage + 1;
        }
        for (int i = 0; i < numberOfPages; i++) {
            listOfPages.add(i);
        }
        return listOfPages;
    }

    /**
     * @return the employeeDAO
     */
    public IEmployeeDAO getEmployeeDAO() {
        return employeeDAO;
    }

    /**
     * @param employeeDAO
     *            the employeeDAO to set
     */
    public void setEmployeeDAO(IEmployeeDAO employeeDAO) {
        this.employeeDAO = employeeDAO;
    }

    /**
     * @return the msg
     */
    public boolean getMsg() {
        return msg;
    }

    /**
     * @param msg
     *            the msg to set
     */
    public void setMsg(boolean msg) {
        this.msg = msg;
    }

    /**
     * @return the pages
     */
    public List<Integer> getPages() {
        return pages;
    }

    /**
     * @param pages
     *            the pages to set
     */
    public void setPages(List<Integer> pages) {
        this.pages = pages;
    }

    /**
     * @return the page
     */
    public String getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(String page) {
        this.page = page;
    }

}
